package com.hotchpotch.activity;

import android.support.annotation.NonNull;
import android.support.design.widget.BottomNavigationView;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.MenuItem;

import com.hotchpotch.R;
import com.hotchpotch.adapter.ViewPagerStateAdapter;
import com.hotchpotch.base.BaseActivity;
import com.hotchpotch.base.BaseFragment;
import com.hotchpotch.custom.CustomViewPager;
import com.hotchpotch.fragment.TestFragment;
import com.hotchpotch.view.BottomNavigationViewHelper;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2017/8/25 0025.
 *
 */

public class BottomNavigationbarActivity extends BaseActivity {
    private static final String TAG = "底部Navigationbar";
    private BottomNavigationView bottomNavigationView;
    private CustomViewPager viewPager;
    private ViewPagerStateAdapter viewPagerStateAdapter;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_bottom_nav;
    }

    @Override
    protected void initView() {
        bottomNavigationView = (BottomNavigationView) findViewById(R.id.bottom_nav);
        bottomNavigationView.setSelectedItemId(R.id.one);//根据具体情况调用 系统默认第一个
        bottomNavigationView.setItemIconTintList(null);//显示图标本身的颜色
        //默认 >3 的选中效果会影响ViewPager的滑动切换时的效果，故利用反射去掉
        BottomNavigationViewHelper.disableShiftMode(bottomNavigationView);
        viewPager = (CustomViewPager) findViewById(R.id.viewpager);
        initViewPager();
    }

    @Override
    protected void initEvents() {
        bottomNavigationView.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem item) {
                switch (item.getItemId()) {
                    case R.id.one:
                        viewPager.setCurrentItem(0);
                        break;
                    case R.id.two:
                        viewPager.setCurrentItem(1);
                        break;
                    case R.id.three:
                        viewPager.setCurrentItem(2);
                        break;
                    case R.id.four:
                        viewPager.setCurrentItem(3);
                        break;
                }
                return true;
            }
        });
        viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                int resId = bottomNavigationView.getMenu().getItem(position).getItemId();
                bottomNavigationView.setSelectedItemId(resId);
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

    @Override
    protected void initData() {

    }

    private List<Fragment> fragmentList = new ArrayList<>();
    private TestFragment fragment1 = TestFragment.newInstance("这是首页");
    private TestFragment fragment2 = TestFragment.newInstance("这是分类页");
    private TestFragment fragment3 = TestFragment.newInstance("这是购物车");
    private TestFragment fragment4 = TestFragment.newInstance("这是个人中心");

    private void initViewPager() {
        viewPager.setCanScroll(true);
        viewPager.setOffscreenPageLimit(4);
        fragmentList.add(fragment1);
        fragmentList.add(fragment2);
        fragmentList.add(fragment3);
        fragmentList.add(fragment4);
        viewPagerStateAdapter = new ViewPagerStateAdapter(getSupportFragmentManager(), fragmentList);
        viewPager.setAdapter(viewPagerStateAdapter);
    }
}
